﻿using System;
using System.Windows.Forms;

namespace DO.DreamOnTrader.Quote
{

	internal enum QuoteType
	{
		股票市況 = 133,
		期貨市況 = 147,
		選擇權市況 = 152,
		興櫃 = 162,
		股票分時 = 134,
		期貨分時 = 149,
		選擇權分時 = 153
	}

	/// <summary>
	/// 讀取tick的抽象類別
	/// </summary>
	public abstract class TickLoader : Form, IQutoeLoader
	{

		#region ---------變數

		/// <summary>
		/// 用於過濾tick的產品別，null時則不過濾
		/// </summary>
		public Product filterProduct = null;

		/// <summary>
		/// 求交易月份(getQuoteMonth())時會用到，0為近月，1月近月+1個月，預設為0
		/// </summary>
		public int addQuoteMonth = 0;

		/// <summary>
		/// 接收報價到的最後交易日的13時幾分，預設為30，超過則會換月
		/// </summary>
		public int filterMinute = 30;

		#endregion

		#region ---------檢查tick的方法

		/// <summary>
		/// 過濾tick的產品別
		/// </summary>
		/// <param name="tick"></param>
		/// <returns></returns>
		protected bool checkProduct(Tick tick)
		{
			if (filterProduct == null || tick.DealProduct == filterProduct)
				return true;
			else
				return false;
		}

		/// <summary>
		/// 過濾tick的合約月份
		/// </summary>
		/// <param name="tick"></param>
		/// <returns></returns>
		protected bool checkCloseMonth(Tick tick)
		{
			if (tick.CloseMonth == getQuoteMonth(tick.TradeTime))
				return true;
			else
				return false;
		}

		/// <summary>
		/// 求出傳入參數時間Time當下的交易月份，依addQuoteMonth決定是近or遠月，依filterMinute幾分決定是否換月
		/// </summary>
		/// <param name="Time">傳入時間</param>
		/// <returns>string "201202"</returns>
		protected string getQuoteMonth(DateTime Time)
		{
			//取得最後交易時間
			DateTime temp = Tick.getCloseDate(Time.Year.ToString() + Time.Month.ToString().PadLeft(2, '0'));
			//取得自定的最後報價換月時間
			DateTime self = new DateTime(temp.Year, temp.Month, temp.Day, temp.Hour, filterMinute, temp.Second);

			DateTime _dt = (temp < self) ? temp : self;

			//判斷是否有超過
			if (Time < _dt)
			{
				//加上tradeMonth
				_dt = _dt.AddMonths(addQuoteMonth);
				return _dt.Year.ToString() + _dt.Month.ToString().PadLeft(2, '0');
			}
			else
			{
				//加上tradeMonth
				_dt = _dt.AddMonths(addQuoteMonth + 1);
				return _dt.Year.ToString() + _dt.Month.ToString().PadLeft(2, '0');
			}
		}

		#endregion

		/// <summary>
		/// 開始Load Tick
		/// </summary>
		/// <returns></returns>
		public abstract bool Start();

		/// <summary>
		/// 停止Load Tick
		/// </summary>
		/// <returns></returns>
		public abstract bool Stop();

		/// <summary>
		/// 處理Tick
		/// </summary>
		/// <param name="tick"></param>
		protected abstract void proecessTick(Tick tick);


		/// <summary>
		/// Load Tick結束之後的處理
		/// </summary>
		/// <param name="tick"></param>
		protected abstract void toEndQuote();

	}

}
